package org.apache.ibatis.demo;

import org.apache.ibatis.demo.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class SqlExecuteDemo {

    public static void main(String[] args) throws IOException {
        SqlExecuteDemo demo = new SqlExecuteDemo();
        demo.executeBySqlSession();
    }

    private void executeBySqlSession() throws IOException {
        // 将配置文件读取到字节流
        InputStream config = Resources.getResourceAsStream("org\\apache\\ibatis\\demo\\conf\\mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("org.apache.ibatis.demo.UserDao.selectAll");
        users.forEach(System.out::println);
    }

}
